﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Followtarget : MonoBehaviour
{

    public Transform plane;
    public float distance;
    public float hight;

    private float hightDamping;
    private float rotationDamping;

    void Update ()
    {
        if (plane != null)
        {
            //镜头拉伸
            if (plane.gameObject.GetComponent<PlaneController>().GetIsRush() && this.GetComponent<Camera>().fieldOfView >= 50f)
            {
                this.GetComponent<Camera>().fieldOfView -= Time.deltaTime * 40;
            }
            else if (!plane.gameObject.GetComponent<PlaneController>().GetIsRush() && this.GetComponent<Camera>().fieldOfView <= 70f)
            {
                this.GetComponent<Camera>().fieldOfView += Time.deltaTime * 20;
            }
        }
	}

    private void LateUpdate()
    {
        if (plane != null)
        {
            //相机跟随
            float x, y, z;
            x = plane.position.x - distance * Mathf.Sin(plane.eulerAngles.y / 180 * Mathf.PI);
            z = plane.position.z - distance * Mathf.Cos(plane.eulerAngles.y / 180 * Mathf.PI);
            y = plane.position.y + hight;
            transform.position = new Vector3(x, y, z);
            transform.rotation = Quaternion.Euler(plane.eulerAngles.x * 0.3f, plane.eulerAngles.y, 0);
            transform.LookAt(plane);
        }
    }
}
